package com.kaizzi.data.jdl.helper;

import com.jfinal.plugin.activerecord.Page;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.TeamListService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData29 {

    @Resource
    private TeamListService teamListService;

    public String getData(ExcelModel model){
//        List<String> dateList = DateUtil.getDateList(1);
//        List<String> transTime = DateUtil.getTransTime(dateList.get(0));
//        String yearFirstDate = DateUtil.getYearFirstDate();
        List<String> ytdDay = DateUtil.getYtdDay1();

        List<TempBeen> ytdTeamSportType = teamListService.getYtdTeamSportType(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> ytdTeamFrequency = teamListService.getYtdTeamFrequency(ytdDay.get(0), ytdDay.get(1));
        List<TempBeen> ytdTeamFrequency2 = teamListService.getYtdTeamFrequency2(ytdDay.get(0), ytdDay.get(1));
        ytdTeamFrequency.addAll(ytdTeamFrequency2);

        ytdTeamSportType.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        ytdTeamFrequency.sort(Comparator.comparing(TempBeen::getTempCount).reversed());

        String sportType = "";
        if (ytdTeamSportType.size() > 2){
            sportType = ytdTeamSportType.get(0).getTempName() +"，" +ytdTeamSportType.get(1).getTempName() +"，" + ytdTeamSportType.get(2).getTempName();
        }
        String rate = "0";
        if (ytdTeamFrequency.size()>0){
            double sum = ytdTeamFrequency.stream().mapToDouble(TempBeen::getTempCount).sum();
            if (sum != 0){
                rate = StringUtil.pointFormat((double) ytdTeamFrequency.get(0).getTempCount() / sum *100);
            }
        }

        String typeTitle = "累计数据表现：'"+sportType +"'的组队需求较高";
        String frequencyTitle = "累计数据表现：'"+rate+"%的用户'使用一键组队超过'"+ytdTeamFrequency.get(0).getTempName()+"'";

        Page29 page29 = new Page29();
        page29.setYtdTeamSportType(ytdTeamSportType);
        page29.setYtdTeamFrequency(ytdTeamFrequency);
        page29.setTitle(LogicUtil.getTitle(model,29));
        page29.setTypeTitle(typeTitle);
        page29.setFrequencyTitle(frequencyTitle);

        return JsonUtil.serialize(page29);
    }

    @Data
    private static class Page29{
        private String title;
        private List<TempBeen> ytdTeamSportType;
        private List<TempBeen> ytdTeamFrequency;
        private String dataPeriod;
        private String typeTitle;
        private String frequencyTitle;
    }
}
